package com.wolfpire.workflow.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.workflow.dao.HolidayDao;
import com.wolfpire.workflow.om.Holiday;


@Repository
public class HolidayDaoImpl extends BaseHibernateDao<Holiday, Long> implements HolidayDao {

	public Date getWorkDays(java.util.Date curDate, Integer days) throws Exception {
		return curDate;

		/*Session session = sessionFactory.openSession();
		Transaction tx = session.beginTransaction();
		Connection conn = session.connection();
		ResultSet rs = null;
		CallableStatement call = conn
				.prepareCall("{Call csms.work_days(?, ?, ?)}");
//		/java.util.Date d = new java.util.Date();
		java.sql.Date operatorTime = new java.sql.Date(curDate.getTime());
		call.setDate(1, operatorTime);// DateUtils.getCurDate(curDate));
		call.setInt(2, days);
		call.registerOutParameter(3, Types.DATE);
		rs = call.executeQuery();
		Date keycode = call.getDate(3);// .getDate("workDate");
		rs.close();
		session.close();

		return keycode;*/
	}
	/**
	 * 通过时间和状态查询节假日或者补班日
	 * @param date日期,可以是年如2014年,可以是年月如2014-05,可是是日如2014-05-01,年月日之间的区分一定要使用“-”
	 * @param status假日状态，0放假，1补班
	 * @return
	 * @throws ParseException 
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryHolidayByDateAndStatus(String date,String status) throws ParseException {
		
		StringBuffer sql=new StringBuffer("");
		sql.append("select  t.limit_id,to_char(t.holiday_time,'yyyy-MM-dd') holiday_time,t.overview,t.status,t.is_legal,t.name ");
		sql.append(" from WF_HOLIDAY t ");
		sql.append(" where 1=1  ");
		if (null!=date) {
			SimpleDateFormat sdf = null;
			String[] dateList = date.split("-");
			if (dateList.length==3) {
				sdf=new SimpleDateFormat("yyyy-MM-dd");
			}
			else if (dateList.length==1) {
				sdf=new SimpleDateFormat("yyyy");
			}
			else if (dateList.length==2) {
				sdf=new SimpleDateFormat("yyyy-MM");
			}
			Date dateTime=sdf.parse(date);
			date=sdf.format(dateTime);
			sql.append(" and to_char(t.holiday_time,'yyyy-MM-dd') like '%"+date+"%' ");
		}
		if (null!=status) {
			sql.append(" and t.status="+status);
		}	
		Query query = getSession().createSQLQuery(sql.toString());
		return query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
				.list();
	}
	
	public Holiday getHoliday(Date holidayTime) {
		Criteria criteria = createCriteria(Restrictions
				.like("holiday_time", holidayTime));
		return (Holiday) criteria.uniqueResult();
	}
	
	public Object getHolidayByHolidayTime(String holidayTime) throws ParseException {
		StringBuffer sql=new StringBuffer("");
		sql.append("select  t.limit_id ");
		sql.append(" from  WF_HOLIDAY t ");
		sql.append(" where 1=1  ");
		if (null!=holidayTime) {
			SimpleDateFormat sdf = null;			
				sdf=new SimpleDateFormat("yyyy-MM-dd");				
			Date dateTime=sdf.parse(holidayTime);
			holidayTime=sdf.format(dateTime);
			sql.append(" and to_char(t.holiday_time,'yyyy-MM-dd') like '%"+holidayTime+"%' ");
		}
		Query query = getSession().createSQLQuery(sql.toString());
		return query.uniqueResult();
	}
}
